#!/use/bin/python
# coding=utf-8
# 用于全量保存指定的日志
import datetime
import pymysql

from dbutils.pooled_db import PooledDB



# proactive_service_conf 数据源
def getConfConnection():
    # 开发环境
    # pool = PooledDB(pymysql, 1, host='172.20.135.96', user='pushdb', passwd='SkYWOrTh$TcOs',
    #                db='proactive_service_conf',
    #                port=3306)  # 1为连接池里的最少连接数
    # 测试环境
    pool = PooledDB(pymysql, 1, host='172.20.150.109', user='test_dmp', passwd='DghHC3lFM1KzT3ZJ',
                db='proactive_service_conf', port=3307)  # 1为连接池里的最少连接数
    # pool = PooledDB(pymysql,1,host='127.0.0.1',user='root',passwd='root',db='life_assistant_data',port=3306) # 5为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur

# proactive_service_data 数据源
def getDataConnection():
    # 开发环境
    #pool = PooledDB(pymysql, 1, host='172.20.151.80', user='pushdb', passwd='SkYWOrTh$TcOs',
    #                db='proactive_service_data',
    #                port=3306)  # 1为连接池里的最少连接数
    # 测试环境
    pool = PooledDB(pymysql, 1, host='172.20.154.103', user='test_dmp', passwd='DghHC3lFM1KzT3ZJ',
                    db='proactive_service_data', port=3407)  # 1为连接池里的最少连接数
    # pool = PooledDB(pymysql,1,host='127.0.0.1',user='root',passwd='root',db='life_assistant_data',port=3306) # 5为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur


def saveAnalysisLog(yyMMddDate):
    try:
        conn, cur = getDataConnection()
        insertSql = f"""
                    insert into log_analysis(log_id,aa_id,active_id,`key`,log_time,create_time)
                     select log_id,aa_id,active_id,`key`,log_time,create_time from log_common_service_{yyMMddDate} t where t.key ='saEnterPage'
                """
        cur.execute(insertSql)
        conn.commit()
        selectSql = "select count(1) from log_analysis"
        cur.execute(selectSql)
        res = cur.fetchone()
        print(f"------------------{yyMMddDate}:当前log_analysis数据量", res[0], "--------------------------")
    except Exception as e:
        print(e)

    finally:
        cur.close()
        conn.close()

#如 2022年12月13号 获取 221213 作为分表后缀
def getdateyyMMdd(beforeOfDay):
    today = datetime.datetime.now()
    # 计算偏移量
    offset = datetime.timedelta(days=-beforeOfDay)
    # 获取想要的日期的时间
    re_date = (today + offset).strftime('%Y%m%d')
    re_date = re_date[2:]
    return re_date


if __name__ == '__main__':
    d = 1
    yyMMddDate = getdateyyMMdd(d)
    saveAnalysisLog(yyMMddDate)